<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>非父子组件传值</title>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="root">
    <child content="hahaha"></child>
    <child content="wowowowo"></child>
  </div>

  <script>
    Vue.prototype.bus = new Vue();

    Vue.component('child', {
      data: function() {
        return {
          selfCont: this.content
        }
      },
      props: {
        content: String
      },
      template: '<div @click="handleClick">{{selfCont}}</div>',
      methods: {
        handleClick: function() {
          // alert(this.selfCont)
          // 向外触发事件
          this.bus.$emit('change', this.selfCont)
        }
      },
      mounted: function() {
        var _this = this;
        // 触发事件监听
        this.bus.$on('change', function(val) {
          _this.selfCont = val
        })
      }
    })
    var vm = new Vue({
      el: '#root',
      data: {

      }
    
    })
    
  </script>
</body>
</html>